import 'dart:convert';
import 'package:blurb/common/models/comment.dart';
import 'package:shared_preferences/shared_preferences.dart';

class CommentService {
  static const String _commentListKey = 'comment_list';

  // 初始化默认评论
  static Future<void> initDefaultComments() async {
    final prefs = await SharedPreferences.getInstance();
    final commentListStr = prefs.getString(_commentListKey);

    // 如果评论列表为空，创建默认评论
    if (commentListStr == null) {
      final DateTime now = DateTime.now();
      final List<Comment> defaultComments = [
        // 分享1的评论
        Comment(
          id: 1,
          content: 'This sunscreen looks amazing! Where did you buy it?',
          time: now.subtract(const Duration(hours: 2)).toIso8601String(),
          userId: 2,
          shareId: 1,
        ),
        Comment(
          id: 2,
          content: 'I\'ve been using this for months, it\'s really good for sensitive skin!',
          time: now.subtract(const Duration(hours: 1)).toIso8601String(),
          userId: 3,
          shareId: 1,
        ),
        
        // 分享2的评论
        Comment(
          id: 3,
          content: 'Does it leave a white cast?',
          time: now.subtract(const Duration(minutes: 45)).toIso8601String(),
          userId: 4,
          shareId: 2,
        ),
        Comment(
          id: 4,
          content: 'I need to try this spray, looks convenient for reapplication!',
          time: now.subtract(const Duration(minutes: 30)).toIso8601String(),
          userId: 1,
          shareId: 2,
        ),
        // 分享4的评论
        Comment(
          id: 5,
          content: 'I love this sunscreen, it really works for me!',
          time: now.subtract(const Duration(minutes: 25)).toIso8601String(),
          userId: 2,
          shareId: 4,
        ),
        Comment(
          id: 6,
          content: 'Is it suitable for oily skin?',
          time: now.subtract(const Duration(minutes: 20)).toIso8601String(),
          userId: 3,
          shareId: 4,
        ),

        // 分享6的评论
        Comment(
          id: 7,
          content: 'My kids like this sunscreen, very gentle.',
          time: now.subtract(const Duration(minutes: 15)).toIso8601String(),
          userId: 4,
          shareId: 6,
        ),
        Comment(
          id: 8,
          content: 'Where did you buy this children sunscreen?',
          time: now.subtract(const Duration(minutes: 10)).toIso8601String(),
          userId: 1,
          shareId: 6,
        ),

        // 分享7的评论
        Comment(
          id: 9,
          content: 'No alcohol and fragrance, perfect for my skin!',
          time: now.subtract(const Duration(minutes: 8)).toIso8601String(),
          userId: 2,
          shareId: 7,
        ),
        Comment(
          id: 10,
          content: 'Thanks for sharing, I will try it.',
          time: now.subtract(const Duration(minutes: 5)).toIso8601String(),
          userId: 3,
          shareId: 7,
        ),
      ];

      // 将评论列表转换为JSON并保存
      final List<Map<String, dynamic>> commentMapList = defaultComments.map((comment) => comment.toJson()).toList();
      await prefs.setString(_commentListKey, jsonEncode(commentMapList));
    }
  }

  // 获取所有评论
  static Future<List<Comment>> getAllComments() async {
    final prefs = await SharedPreferences.getInstance();
    final commentListStr = prefs.getString(_commentListKey);

    if (commentListStr != null) {
      final List<dynamic> commentMapList = jsonDecode(commentListStr);
      return commentMapList.map((commentMap) => Comment.fromJson(commentMap)).toList();
    }

    return [];
  }

  // 获取特定分享的评论
  static Future<List<Comment>> getShareComments(int shareId) async {
    final comments = await getAllComments();
    return comments.where((comment) => comment.shareId == shareId).toList();
  }

  // 添加新评论
  static Future<bool> addComment(Comment newComment) async {
    final comments = await getAllComments();
    
    // 添加新评论
    comments.add(newComment);
    
    // 保存评论列表
    final prefs = await SharedPreferences.getInstance();
    final List<Map<String, dynamic>> commentMapList = comments.map((comment) => comment.toJson()).toList();
    await prefs.setString(_commentListKey, jsonEncode(commentMapList));
    
    return true;
  }

  // 删除评论
  static Future<bool> deleteComment(int commentId) async {
    final comments = await getAllComments();
    final prefs = await SharedPreferences.getInstance();
    
    // 查找并删除评论
    final index = comments.indexWhere((comment) => comment.id == commentId);
    if (index != -1) {
      // 删除评论
      comments.removeAt(index);
      
      // 保存评论列表
      final List<Map<String, dynamic>> commentMapList = comments.map((comment) => comment.toJson()).toList();
      await prefs.setString(_commentListKey, jsonEncode(commentMapList));
      
      return true;
    }
    
    return false;
  }
} 